<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :rules="queryRules"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          show-word-limit
          clearable
          class="inputClass"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车架号"
          maxlength="17"
          show-word-limit
          clearable
          class="inputClass"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入车型名称"
          maxlength="40"
          show-word-limit
          clearable
          class="inputClass"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentType">
        <el-select
          v-model="queryParams.paymentType"
          placeholder="请选择支付方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          maxlength="40"
          show-word-limit
          clearable
          class="inputClass"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="softwareName">
        <el-input
          v-model="queryParams.softwareName"
          placeholder="请输入商品名称"
          maxlength="40"
          show-word-limit
          clearable
          class="inputClass"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="购买规格" prop="softwareSpec">-->
        <!--<el-input-->
          <!--v-model="queryParams.softwareSpec"-->
          <!--placeholder="请输入购买规格"-->
          <!--maxlength="40"-->
          <!--show-word-limit-->
          <!--clearable-->
          <!--class="inputClass"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="订单来源" prop="orderSource">
        <el-select v-model="queryParams.orderSource" placeholder="请选择订单来源" clearable>
          <el-option
            v-for="dict in dict.type.order_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.order_log_payment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:paymentlog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="tableColumns"></right-toolbar>
    </el-row>
    <el-table
      ref="tables"
      v-loading="loading"
      :data="paymentlogList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" align="center"/>

      <!-- <el-table-column label="主键" align="center" prop="id" v-if="false" /> -->
      <el-table-column
        label="订单号"
        align="center"
        prop="orderNo"
        key="orderNo"
        v-if="tableColumns[0].visible"
      />
      <el-table-column
        label="车架号"
        align="center"
        prop="vin"
        key="vin"
        v-if="tableColumns[1].visible"
      />
      <el-table-column
        label="车型名称"
        align="center"
        prop="modelName"
        key="modelName"
        v-if="tableColumns[2].visible"
      />
      <el-table-column
        label="商品名称"
        align="center"
        prop="softwareName"
        key="softwareName"
        v-if="tableColumns[3].visible"
      />
      <el-table-column
        label="软件包功能项"
        align="center"
        prop="softwareFunction"
        key="softwareFunction"
        v-if="tableColumns[4].visible"
      >
        <template
          slot-scope="scope"
          v-if="scope.row.softwareFunction"
        >{{ scope.row.softwareFunction }}</template>
      </el-table-column>
      <el-table-column
        label="购买规格"
        align="center"
        prop="softwareSpec"
        key="softwareSpec"
        v-if="tableColumns[5].visible"
      >
        <template slot-scope="scope">{{ getSoftwareSpecTxt(scope.row.softwareSpec) }}</template>
      </el-table-column>

      <el-table-column
        label="用户ID"
        align="center"
        prop="userId"
        key="userId"
        v-if="tableColumns[6].visible"
      />
      <el-table-column
        label="购买人昵称"
        align="center"
        prop="userName"
        key="userName"
        v-if="tableColumns[7].visible"
      />

      <el-table-column
        label="类型"
        align="center"
        prop="type"
        key="type"
        v-if="tableColumns[8].visible"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_log_payment_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column
        label="支付流水号"
        align="center"
        prop="transactionId"
        key="transactionId"
        v-if="tableColumns[9].visible"
      />
      <el-table-column
        label="支付额度"
        align="center"
        prop="paymentPrice"
        key="paymentPrice"
        v-if="tableColumns[10].visible"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.paymentPrice == 0">{{ scope.row.paymentPrice }}</el-tag>
          <span v-else-if="scope.row.paymentType == 'DAP'">{{ parseInt(scope.row.paymentPrice) }}</span>
          <span v-else>￥{{ scope.row.paymentPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付方式"
        align="center"
        prop="paymentType"
        key="paymentType"
        v-if="tableColumns[11].visible"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.paymentType || scope.row.paymentType.length == 0">-</span>
          <dict-tag v-else :options="dict.type.pay_type" :value="item" v-for="item in scope.row.paymentType.split(',')" :key="item"/>
        </template>
      </el-table-column>
      <el-table-column
        label="订单来源"
        align="center"
        prop="orderSource"
        key="orderSource"
        v-if="tableColumns[12].visible"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_source" :value="scope.row.orderSource"/>
        </template>
      </el-table-column>

      <el-table-column
        label="耗时"
        align="center"
        prop="time"
        key="time"
        v-if="tableColumns[13].visible"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDate"
        key="createDate"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        v-if="tableColumns[14].visible"
        width="110"
      ></el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        key="payTime"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        v-if="tableColumns[15].visible"
        width="110"
      >
        <template slot-scope="scope">
          {{ scope.row.payTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column
        label="最后更新时间"
        align="center"
        prop="lastUpdateDate"
        key="lastUpdateDate"
        v-if="tableColumns[16].visible"
        width="110"
      ></el-table-column>
      <el-table-column
        label="最后更新人"
        align="center"
        prop="lastUpdateBy"
        key="lastUpdateBy"
        width="110"
        v-if="tableColumns[17].visible"
      />

      <el-table-column
        label="请求报文"
        align="center"
        prop="reqMessage"
        key="reqMessage"
        v-if="tableColumns[18].visible"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="checkMessage(scope.row.reqMessage)"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="响应报文"
        align="center"
        prop="respMessage"
        key="respMessage"
        v-if="tableColumns[19].visible"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="checkMessage(scope.row.respMessage)"
          >查看</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:paymentlog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:paymentlog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单主表Id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单主表Id"/>
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.order_log_payment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单金额" prop="paymentPrice">
          <el-input v-model="form.paymentPrice" placeholder="请输入订单金额"/>
        </el-form-item>
        <el-form-item label="请求报文" prop="reqMessage">
          <el-input v-model="form.reqMessage" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="响应报文" prop="respMessage">
          <el-input v-model="form.respMessage" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="耗时" prop="time">
          <el-input v-model="form.time" placeholder="请输入耗时"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker
            clearable
            v-model="form.createDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择创建时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="lastUpdateDate">
          <el-date-picker
            clearable
            v-model="form.lastUpdateDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择最后更新时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新人" prop="lastUpdateBy">
          <el-input v-model="form.lastUpdateBy" placeholder="请输入最后更新人"/>
        </el-form-item>
        <el-form-item label="有效标识" prop="deleteFlag">
          <el-input v-model="form.deleteFlag" placeholder="请输入有效标识"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看报文的dialog -->
    <el-dialog title="查看报文" width="50%" :visible.sync="showCheckMsg">
      <div>{{ showingMsg }}</div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCheckMsg = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPaymentlog,
  getPaymentlog,
  delPaymentlog,
  addPaymentlog,
  updatePaymentlog
} from "@/api/shop/paymentlog";

export default {
  name: "Paymentlog",
  dicts: ["order_log_payment_type", "order_payment_type", "order_source","pay_type"],
  data() {
    return {
      // table列筛选
      tableColumns: [
        { key: 0, label: `订单号`, visible: true },
        { key: 1, label: `车架号`, visible: true },
        { key: 2, label: `车型名称`, visible: true },
        { key: 3, label: `商品名称`, visible: true },
        { key: 4, label: `软件包功能项`, visible: true },
        { key: 5, label: `购买规格`, visible: true },
        { key: 6, label: `用户ID`, visible: true },
        { key: 7, label: `购买人昵称`, visible: true },
        { key: 8, label: `类型`, visible: true },
        { key: 9, label: `支付流水号`, visible: true },
        { key: 10, label: `支付额度`, visible: true },
        { key: 11, label: `支付方式`, visible: true },
        { key: 12, label: `订单来源`, visible: true },
        { key: 13, label: `耗时`, visible: true },
        { key: 14, label: `创建时间`, visible: true },
        { key: 15, label: `支付时间`, visible: true },
        { key: 16, label: `最后更新时间`, visible: true },
        { key: 17, label: `最后更新人`, visible: true },
        { key: 18, label: `请求报文`, visible: true },
        { key: 19, label: `响应报文`, visible: true }
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付流水表格数据
      paymentlogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 默认排序
      defaultSort: { prop: "createDate", order: "descending" },
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        orderNo: undefined,
        type: undefined,
        paymentType: undefined,
        paymentPrice: undefined,
        reqMessage: undefined,
        respMessage: undefined,
        time: undefined,
        createDate: undefined,
        lastUpdateDate: undefined,
        lastUpdateBy: undefined,
        deleteFlag: undefined,
        userId: undefined,
        vin: undefined,
        modelName: undefined,
        softwareName: undefined,
        softwareSpec: undefined,
        orderSource: undefined,
        startPaymentTime: undefined,
        endPaymentTime: undefined,
        ids: undefined,
        orderByColumn: "createDate",
        isAsc: "descending"
      },
      //  查询参数校验
      queryRules: {
        orderNo: [
          {
            validator: (rule, value, callback) => {
              if (value.length > 0 && !new RegExp(/^\d{0,40}$/).test(value)) {
                this.queryParams.orderNo = undefined;
                callback(new Error("请输入正确的订单号"));
              } else if (value.length > 40) {
                callback(new Error("订单号的长度不能超过40"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ],
        // userId: [
        //   {
        //     validator: (rule, value, callback) => {
        //       if (value.length > 0 && !new RegExp(/^\d{0,10}$/).test(value)) {
        //         this.queryParams.userId = undefined;
        //         callback(new Error("请输入正确的用户ID"));
        //       } else if (value.length > 10) {
        //         callback(new Error("用户Id的长度不能超过10"));
        //       } else {
        //         callback();
        //       }
        //     },
        //     trigger: "blur"
        //   }
        // ],
        vin: [
          {
            validator: (rule, value, callback) => {
              if (
                value.length > 0 &&
                !new RegExp(/^[A-HJ-NPR-Z\d]{0,17}$/).test(value)
              ) {
                this.queryParams.vin = undefined;
                callback(new Error("请输入正确的VIN"));
              } else if (value.length > 17) {
                callback(new Error("VIN的长度不能超过17"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "主键不能为空", trigger: "blur" }],
        orderId: [
          { required: true, message: "订单主表Id不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        paymentPrice: [
          { required: true, message: "订单金额不能为空", trigger: "blur" }
        ],
        reqMessage: [
          { required: true, message: "请求报文不能为空", trigger: "blur" }
        ],
        respMessage: [
          { required: true, message: "响应报文不能为空", trigger: "blur" }
        ],
        time: [{ required: true, message: "耗时不能为空", trigger: "blur" }],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        lastUpdateDate: [
          { required: true, message: "最后更新时间不能为空", trigger: "blur" }
        ],
        lastUpdateBy: [
          { required: true, message: "最后更新人不能为空", trigger: "blur" }
        ],
        deleteFlag: [
          { required: true, message: "有效标识不能为空", trigger: "blur" }
        ]
      },
      // 查看报文
      showCheckMsg: false,
      showingMsg: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getSoftwareSpecTxt(softwareSpec){
      if(softwareSpec[0] != '{') return softwareSpec
      let softwareSpecData = JSON.parse(softwareSpec);
      return softwareSpecData.name1 + (softwareSpecData.name2?(','+softwareSpecData.name2):'') + (softwareSpecData.name3?(','+softwareSpecData.name3):'')
    },
    /** 查询支付流水列表 */
    getList() {
      this.loading = true;
      this.queryParams.ids = undefined;
      if (this.dateRange != "" && this.dateRange != null) {
        this.queryParams.startPaymentTime = this.dateRange[0];
        this.queryParams.endPaymentTime = this.dateRange[1];
      } else {
        this.queryParams.startPaymentTime = undefined;
        this.queryParams.endPaymentTime = undefined;
      }

      listPaymentlog(this.queryParams).then(response => {
        this.paymentlogList = []
        this.$nextTick(()=>{
          console.log(response.rows)
          this.paymentlogList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        orderId: undefined,
        orderNo: undefined,
        type: undefined,
        paymentPrice: undefined,
        reqMessage: undefined,
        respMessage: undefined,
        time: undefined,
        createDate: undefined,
        createBy: undefined,
        lastUpdateDate: undefined,
        lastUpdateBy: undefined,
        deleteFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.ids = undefined;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.queryParams.ids = undefined;
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.queryParams.ids = undefined;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getPaymentlog(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改支付流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePaymentlog(this.form)
              .then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addPaymentlog(this.form)
              .then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除支付流水编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delPaymentlog(ids);
        })
        .then(() => {
          this.loading = false;
          this.handleQuery();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.ids = undefined;
      if (this.ids.length > 0) {
        if (this.ids.length > 1000) {
          this.$message.error("勾选导出的数据不能超过一千条！");
          return false;
        }
        let selectionIds = "";
        this.ids.forEach(id => {
          selectionIds += id + ",";
        });
        if (selectionIds.length > 0) {
          selectionIds = selectionIds.substring(0, selectionIds.length - 1);
        }
        this.queryParams.ids = selectionIds;
      }
      this.download(
        "shop/paymentlog/export",
        {
          ...this.queryParams
        },
        `paymentlog_${new Date().getTime()}.xlsx`
      );
    },
    /**
     * 查看报文
     */
    checkMessage(msg) {
      this.showCheckMsg = true;
      if (msg) {
        this.showingMsg = JSON.parse(msg);
      }
      console.log("this.showingMsg: ", this.showingMsg);
    }
  }
};
</script>
<style scoped>
::v-deep .el-dialog {
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  margin: 0px !important;
}
.inputClass {
}
/* element-ui input style overload */
::v-deep .inputClass > input {
  padding-right: 70px;
}
</style>
